﻿//using Autofac.Features.OwnedInstances;
using DFGJ.WMS_PDA.WebAPI.CfgFile;
using DFGJ.WMS_PDA.WebAPI.Extensions;
using DFGK.WMS.Common.Extensions;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.PDA;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NetTaste;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.Transactions;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    /// <summary>
    /// PDA出库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDAOutboundController : ApiBaseController
    {

        private readonly DFGK_DB dbContext;
        private ITOutboundOrderDetailRepository TOutboundOrderDetail;
        private IPDAStackingRepository StackingRepository;
        private readonly ITLogRepository tLog;

        public PDAOutboundController(DFGK_DB dbContext, ITOutboundOrderDetailRepository TOutboundOrderDetail, IPDAStackingRepository StackingRepository,ITLogRepository tLog)
        {
            this.dbContext = dbContext;
            this.TOutboundOrderDetail = TOutboundOrderDetail;
            this.StackingRepository = StackingRepository;
            this.tLog = tLog;
        }

      

        #region 获取出库单明细Plus
        /// <summary>
        /// 获取出库单明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTShippingOrderDetailPlus(int pageIndex, int pageSize, int isNetWork)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            var list = TOutboundOrderDetail.GetTShippingOrderDetailPlus(pageIndex,pageSize, isNetWork);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));

            }

            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }
        #endregion

        #region 出库复核 最终确定按钮  海宁交通
        /// <summary>
        /// 出库复核 最终确定按钮
        /// </summary>
        /// <param name="OrderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [TypeFilter(typeof(LogActionFilter))]
        public IActionResult SubmitOutboudOrder(SubmitOutListDTONoNet OrderDTO)
        {
            string message;
            try
            {
                var resOut = TOutboundOrderDetail.ChukuMsg(OrderDTO, out message, out List<TLog> logs);
                if (logs.Count>0) 
                {
                    foreach (var item in logs)
                    {
                        tLog.Add(item);
                    }
                }
                if (resOut)
                {
                    return Ok(BaseResult.Result("200", "出库解绑成功"));
                }
                else
                {
                    return Ok(BaseResult.Result("500", message));
                }
            }
            catch (Exception ex)
            {
                tLog.Add(new TLog()
                {
                    ActionType = 0,
                    CreateDate = DateTime.Now,
                    Description = ex.Message,
                    LogType = 1,
                });
                return Ok(BaseResult.Result("500", ex.Message));
            }
  
        }
        #endregion

        #region 出库复核 最终确定按钮 无网版本  海宁交通
        /// <summary>
        /// 出库复核 最终确定按钮
        /// </summary>
        /// <param name="OrderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [TypeFilter(typeof(LogActionFilter))]
        public IActionResult SubmitOutboudOrderNoNet(List<SubmitOutListDTONoNet> SubmitOutListDTONoNetList)
        {
            string message;
            try
            {
                var chukuRes = true;
                List<string> errOrderSn=new List<string>();
                foreach (var OrderDTO in SubmitOutListDTONoNetList)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      {
                    var resOut = TOutboundOrderDetail.ChukuMsgNoNet(OrderDTO, out message, out List<TLog> logs);
                    if (logs.Count > 0)
                    {
                        foreach (var item in logs)
                        {
                            tLog.Add(item);
                        }
                        if (resOut == false)
                        {
                            chukuRes = false;
                            errOrderSn.Add("单号："+OrderDTO.orderSn+"错误信息"+message);
                        }
                    }
                }
                if (chukuRes)
                {
                    return Ok(BaseResult.Result(true, "出库解绑成功", "200"));
                }
                else
                {
                    return Ok(BaseResult.Result(false, string.Join(",", errOrderSn),"200"));
                }
            }
            catch (Exception ex)
            {
                tLog.Add(new TLog()
                {
                    ActionType = 0,
                    CreateDate = DateTime.Now,
                    Description = ex.Message,
                    LogType = 1,
                });
                return Ok(BaseResult.Result("500", ex.Message));
            }

        }
        #endregion

    }
}
